require_relative 'ms_availability_status'
require_relative 'tag'
require_relative 'tlv_error'

module SmppClient

  module Pdu

    module Tlv

      # Parse TLV and control tag to Tlv mapping.

      module TlvFactory

        @tags = {
                Tag::MS_AVAILABILITY_STATUS => MsAvailabilityStatus
        }

        def self.parse(byte_buffer)
          tag = byte_buffer.read_short
          clazz = @tags[tag]
          raise TlvError, "Tlv corresponding to tag #{tag} is not found." if clazz == nil
          clazz.new(byte_buffer)
        end

        def self.add(const_name, const_value, clazz)
          if @tags.has_key?(const_value)
            raise TlvError, "Tag #{const_value} already mapped to #{@tags[const_value]} class."
          end
          Tag.const_set(const_name, const_value)
          @tags[const_value] = clazz
        end

        def self.remove(const)
          @tags.delete(const)
        end

      end

    end

  end

end